package net.xiduth.game.npc.others;
 
import net.xiduth.game.Animation;
import net.xiduth.game.Entity;
import net.xiduth.game.Graphics;
import net.xiduth.game.Hit;
import net.xiduth.game.Hit.HitLook;
import net.xiduth.game.World;
import net.xiduth.game.WorldTile;
import net.xiduth.game.npc.combat.NPCCombatDefinitions;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;
 
@SuppressWarnings("serial")
public class Death extends DeathNPC {
 
    public Death(int id, WorldTile tile) {
        super(id, tile);
    }
 
    @Override
    public void sendDeath(Entity source) {
        final NPCCombatDefinitions defs = getCombatDefinitions();
        resetWalkSteps();
        getCombat().removeTarget();
        setNextAnimation(null);
        final WorldTile tile = this;
        WorldTasksManager.schedule(new WorldTask() {
            int loop;
 
            @Override
            public void run() {
                if (loop == 0) {
                    setNextAnimation(new Animation(defs.getDeathEmote()));
                    setNextGraphics(new Graphics(2924 + getSize()));
                } else if (loop >= defs.getDeathDelay()) {
                    reset();
                    new DeathNPC(1, tile);
                    if(World.canMoveNPC(getPlane(), tile.getX()+1, tile.getY(), 1)) 
                        tile.moveLocation(1, 0, 0);
                    else if(World.canMoveNPC(getPlane(), tile.getX()-1, tile.getY(), 1)) 
                        tile.moveLocation(-1, 0, 0);
                    else if(World.canMoveNPC(getPlane(), tile.getX(), tile.getY()-1, 1)) 
                        tile.moveLocation(0, -1, 0);
                    else if(World.canMoveNPC(getPlane(), tile.getX(), tile.getY()+1, 1)) 
                        tile.moveLocation(0, 1, 0);
                    new DeathNPC(1, tile);
                    finish();
                    stop();
                }
                loop++;
            }
        }, 0, 1);
    }
 
    @Override
    public void removeHitpoints(Hit hit) {
        super.removeHitpoints(hit);
        if (hit.getLook() != HitLook.MELEE_DAMAGE || hit.getSource() == null)
            return;
        hit.getSource().applyHit(new Hit(this, 10, HitLook.REGULAR_DAMAGE));
    }
}